package UserGift.dao;


import Hibernate_web.HibernateUtil;
import UserGift.model.User;
import org.hibernate.Session;

import java.util.List;

public class UserDao implements IDao<Integer, User> {


    @Override
    public User getById(Integer id) {
        Session session = HibernateUtil.getSessionfactory().getCurrentSession();
        session.beginTransaction();
        User user = (User) session.load(User.class, id);
        session.getTransaction().commit();
        return user;
    }

    @Override
    public List<User> getAll() {
        Session session = HibernateUtil.getSessionfactory().getCurrentSession();
        session.beginTransaction();
        List<User> userList = session.createCriteria(User.class).list();
        session.getTransaction().commit();
        return userList;
    }

    @Override
    public void save(User user) {
        Session session = HibernateUtil.getSessionfactory().getCurrentSession();
        session.beginTransaction();
        session.save(user);
        session.getTransaction().commit();
    }

    @Override
    public void update(User user) {
        Session session = HibernateUtil.getSessionfactory().getCurrentSession();
        session.beginTransaction();
        session.update(user);
        session.getTransaction().commit();
    }
}
